package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.CategoryListAdapter;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.Category;
import org.project.product.android.pfe.fanta.util.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class CategoryListActivity extends Activity {

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.activity_list_category);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initViewAnListen();
	}

	private void initViewAnListen() {
		ListView listView = (ListView) findViewById(R.id.list_category);
		final CategoryListAdapter adapter = new CategoryListAdapter(
		        DataManagerFacade.getInstance(this).getCategoryFromLocal());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
			        long arg3) {
				Category category = (Category) adapter.getItem(arg2);
				if (category.getNumberItem() == 0) {
					Utils.lancerActivity(CategoryListActivity.this,
					        AddProduct.class, category);
				} else {
					Utils.lancerActivity(CategoryListActivity.this,
					        ProductListActivity.class, category);

				}

			}
		});
		ImageView addIcon = (ImageView) findViewById(R.id.img_add_category);
		addIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Utils.lancerActivity(CategoryListActivity.this,
				        AddCategoryActivity.class);

			}
		});

	}
}
